define(function() {
	return {
		isEmpty: function(str) {
			if (typeof str == 'undefined' || str == null || str == "" || str == "NULL" || str == "null" || str == "undefined") {
				return true;
			}
			return false;
		},
		getNetWork: function(url, param, _callback) {
			this.network(url, "GET", param, true, _callback);
		},
		postNetwork: function(url, param, _callback) {
			//默认post方式异步请求
			this.network(url, "POST", param, true, _callback);
		},
		network: function(url, method, param, async, _callback) {
			var $that = this;
			$.ajax({
				type: method,
				url: url,
				data: param,
				async: async,
				success: function(data){
					if (typeof _callback == 'function') {
						_callback(data);
					}
				}
			});
		},
		//js版本 request.getParam();
		locationHref : function(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null)
				return unescape(decodeURI(r[2]));
			return null;
		},
		changeRegion: function(obj, selectedCode) {
			var that_ = this;
			that_.network(ctx + "/common/getCityList.do", "POST", {"param_pcode" : selectedCode}, false, function(res) {
	    		that_.generateSelectOptions(obj, res, "-请选择-", "");
	    	});
		},
		generateSelectOptions: function(obj, res, fritOptionText, selectedCode) {
			var optionArray = res;
			obj.options.length=0;
			var pos = 0;
			if(fritOptionText && fritOptionText != null && fritOptionText != "") {
				obj.options[0] = new Option(fritOptionText,"");
				pos++;
			}
			if(res == null || res == ""){
				return;
			}
			var arrayJson =	eval(res);
			if(arrayJson && arrayJson.length){
				var index = arrayJson.length;
				var tempArray;
				for(var i=0;i<index;i++){
					tempArray = arrayJson[i];
					var tempOption  = new Option(tempArray.name,tempArray.code);
					tempOption.setAttribute("pcode",tempArray.pcode);
					obj.options[i+pos]=tempOption;
					if(selectedCode == tempArray.code){
						obj.selectedIndex = i + pos;
					}
				}
			}
			if (typeof form != 'undefined')
				form.render();
		},
		initSelect: function(selObj, val) {
			for(i=0;i<selObj.length;i++){
				if(val==selObj[i].value){
					selObj.selectedIndex=i;
				}
			}
			if (typeof form != 'undefined')
				form.render();
		},
		dialogEditCallback: function(index, message) {
			layer.close(index);
			layer.msg((message == null || message == "" || typeof message == 'undefined') ? "保存成功" : message);
			setTimeout(function() {
				query();
			}, 2000);
		},
		//obj为checkbox js对象，非jquery对象
		checkboxLength: function(obj) {
			var n = 0;
			for (var i = 0; i < obj.length; i++) {
				if (obj[i].checked) {
					n++;
				}
			}
			return n;
		},
		checkboxVals: function(obj) {
			var arr = [];
			for (var i = 0; i < obj.length; i++) {
				if (obj[i].checked) {
					arr.push(obj[i].value);
				}
			}
			return arr;
		},
		//省市联动初始化
		initCity: function(cityCodePath) {
			if (Common.isEmpty(cityCodePath)) {
				return;
		    }
		    var cityCodes = cityCodePath.split(",");
		    $.each(cityCodes, function(i, o) {
			    if (o == null || o == "" || o == " ") {
					cityCodes.splice(i, 1);
			    }
			});
		    var provinceCode = "";
		    var cityCode = "";
		    if (cityCodes.length >= 1) {
				provinceCode = cityCodes[0];
		    }
		    if (cityCodes.length >= 2) {
				cityCode = cityCodes[1]
		    }
		    if (!Common.isEmpty(provinceCode)) {
				Common.initSelect(document.addForm.province, provinceCode);
		    }
		    if (!Common.isEmpty(cityCode)) {
				Common.changeRegion(document.addForm.city, provinceCode)
				Common.initSelect(document.addForm.city, cityCode);
				if (typeof form != 'undefined')
				form.render();
		    }
		},
		chooseDocument: function(choosemore, type, _callback) {
			if (typeof layer != "undefined") {
			    layer.open({
					type: 2,
					title: '选择图片',
					shade: 0.4, //弹窗后边是否显示阴影 可以填true/false 或者0.？透明度
					shadeClose: true,//点击阴影是否关闭弹窗
					maxmin: true, //开启最大化最小化按钮
					btn: ['确定','关闭'],
					area: ["650px", "550px"],
					offset: (document.body.clientHeight - 570) / 2,//高度
					content: ctx + '/DocumentController/getDialogList.do?choosemore=' + choosemore + "&param_type=" + type,
					yes: function(index){
			        	//当点击‘确定’按钮的时候，获取弹出层返回的值
					    var iframeWin = window["layui-layer-iframe" + index];
			        	var res = iframeWin.callbackdata();
			        	layer.close(index);
			        	if (typeof _callback != 'undefined') {
				        	_callback(res);
			        	}
			    	}
				});
			}
		}
	}
});
